<?php
	
	session_start();
	require("connect.php");
	$link = connect_APO();
		
	require("check.php");
	is_logged_in();
	
	require("header+logoutB.php");
	headerL();
	
	$midweekName=$_GET['midweek_name'];
	$error = $_GET['error'];
	
	$sql = sprintF("SELECT * From midweeks WHERE Name='$midweekName'");
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	
	$midweekStartTime=$row["StartTime"];
	$midweekSName=$row["SName"];
	$stAMPM = "AM";
	$stTimePre = (int)substr($midweekStartTime,0,2);
			if($stTimePre >12){ 
				$stAMPM= "PM";
				$stTimePre=$stTimePre-12; 
			}
	$stTimePost = substr($midweekStartTime,2,3);
	$midweekEndTime=$row["EndTime"];
	$enTimePre = (int)substr($midweekEndTime,0,2);
	$enAMPM = "AM";
			if($enTimePre >12){ 
				$enTimePre=$enTimePre-12; 
				$enAMPM = "PM";
			}
	$enTimePost = substr($midweekEndTime,2,3);
	$midweekDay=$row["Day"];
	$midweekCredit=$row["Credit"];
	$midweekLeader1=$row["Leader1"];
	$ml1N = "";
	$ml2N = "";
	if(strcmp($midweekLeader1,"") != 0){
		$sql2 = sprintF("SELECT Fname, Lname, ID, PhoneNumber FROM `users` JOIN `midweeks` ON `users`.ID = `midweeks`.Leader1 WHERE `users`.ID='$midweekLeader1'");
		$result2 = mysql_query($sql2);
		$row2= mysql_fetch_array($result2);
		$ml1N = $row2["Fname"]." ".$row2["Lname"]." (".$row2["ID"].") ".$row2["PhoneNumber"];
	}
	$midweekLeader2=$row["Leader2"];
	if(strcmp($midweekLeader2,"") != 0){
		$sql2 = sprintF("SELECT Fname, Lname, ID, PhoneNumber FROM `users` JOIN `midweeks` ON `users`.ID = `midweeks`.Leader2 WHERE `users`.ID='$midweekLeader2'");
		$result2 = mysql_query($sql2);
		$row2= mysql_fetch_array($result2);
		$ml2N = $row2["Fname"]." ".$row2["Lname"]." (".$row2["ID"].") ".$row2["PhoneNumber"];
	}
	$midweekMaxSlots=$row["MaxPeople"];
	$location=$row["Location"];
	$meetingPlace=$row["MeetingPlace"];
	$closedBool = $row["Closed"];
	$leader = false;
	
	$leader = is_leader_of_midweek($midweekLeader1,$midweekLeader2);
	
	$sql = sprintf("SELECT * FROM `$midweekName`, users WHERE `$midweekName`.ID = users.ID");
	$result = mysql_query($sql);
	$numberPeople = 0;
	while($row = mysql_fetch_array($result)){
		$numberPeople += 1;
		$personID[] = $row["ID"];
		$personFName[] = $row["Fname"];
		$personLName[] = $row["Lname"];
		$personPhone[] = $row["PhoneNumber"];
		$personDrive[] = $row["Drive"];
	}
	$_SESSION['numberPeopleAtMidweek']=$numberPeople;
	
	$availableSlot = $midweekMaxSlots - $numberPeople;
	if($availableSlot<0){
		$availableSlot=0;
	}
	
	echo "<html>
<head>
</head>
<body>

<font class='mainFont'>
	<center>
		<div id='midweekTable'>
			<div id='links'>
				<div id='backLink'> <A href='index.php'>Back to Calendar</A></div>";
				/*if($leader){
					echo "<div id='emailReminder'> <A href='emailReminder.php?midweek_name=$midweekName''>Email Reminder</A></div>";
				}*/
			
			echo "</div>
			<b class='b1f'></b><b class='b2f'></b><b class='b3f'></b><b class='b4f'></b>
			<div class='contentf'>
				<div id='midweekTName'>$midweekSName</div>
				<div id='midweekTTime'>
					<div id='midweekTSTime'>Start Time: $stTimePre$stTimePost$stAMPM</div>
					<div id='midweekTETime'>End Time: $enTimePre$enTimePost$enAMPM</div></div>
					<div>Location: $location</div>
					<div style='padding-bottom:10'>Meeting Place: $meetingPlace</div>
					<div id='midweekTCredit'>Midweek Credit earned: $midweekCredit </div>
					<div id='midweekTLeaders'>Midweek Leaders:</div>
					<div id='midweekTLeader1'>$ml1N</div>
					<div id='midweekTLeader2'>$ml2N</div>
					<div id='midweekTSlots'>
						<div id='midweekTMSlots'>Max Slots: $midweekMaxSlots</div>
						<div id='midweekTASlots'>Available Slots: $availableSlot</div>
					</div>
					<div id='midweekTPeople'>People Going:</div>
					";
			
			$alreadySignedUp = 0;
			for($i = 0; $i<$numberPeople; $i +=1){
				echo "<div id='fullPerson'><div id='midweekTPerson'><div id='personName'>$personFName[$i] $personLName[$i]</div> <div id='personPhone'>$personPhone[$i]</div>";
				
				if($personDrive[$i] == 1)
					echo " <div id='driving'>Driving</div> ";
						
				echo "</div>";
				if($leader){
					echo "<form action='removeFromMidweek.php?midweek_name=$midweekName' method='post' id='remove'>
					<input type='hidden' id='userID' name='userID' value='$personID[$i]'/>
					<input type='submit' value='Remove' class='loginButton' id='remove'/>
					</form>";
				}
				if(strcmp($personID[$i],$_SESSION['ID'])==0){
					$alreadySignedUp=1;
				}
				echo "</div>";
			}
			$day= date("D");
			if (strcmp($day, "Sun")==0){
				$dayT = "Mon";
			} else if (strcmp($day, "Mon")==0){
				$dayT = "Tue";
			} else if (strcmp($day, "Tue")==0){
				$dayT = "Wed";
			} else if (strcmp($day, "Wed")==0){
				$dayT = "Thu";
			} else if (strcmp($day, "Thu")==0){
				$dayT = "Fri";
			} else if (strcmp($day, "Fri")==0){
				$dayT = "Sat";
			} else if (strcmp($day, "Sat")==0){
				$dayT = "Sun";
			}
			$now= date("H:i:s");
			$nowT = strtotime($now);
			$start= strtotime($midweekStartTime);
			if(strcmp($day,$midweekDay)==0){
				if($nowT>$start){
					$expired=0;
				} else {
					$expired=1;
					$closedBool=1;
					$sql3=sprintF("UPDATE `midweeks` SET `Closed` = '1' WHERE `midweeks`.`Name` = '$midweekName'");
					$result3 = mysql_query($sql3);
				}
			} else if(strcmp($dayT,$midweekDay)==0){
				if($start>$nowT){
					$expired=0;
				} else {
					$expired=1;
					$closedBool=1;
					$sql3=sprintF("UPDATE `midweeks` SET `Closed` = '1' WHERE `midweeks`.`Name` = '$midweekName'");
					$result3 = mysql_query($sql3);
				}
			} else { 
				$expired = 0;
			}
			
			if($leader){
				// The midweek leader should be able to add/remove other people to the list no matter what
				// If they are added or removed from the list it will email the user saying what has happend
				// The  midweek leader should still be able to remove themself from the midweek as well
				if(strcmp($error,"1")==0){
					echo "<div id='midweekTWrong'><b>Sorry, You entered the phone number in the wrong format. Please try again.</b></div>";
				}
				if ($closedBool==1){
					echo "<SCRIPT LANGUAGE='JavaScript'>
function un_check(){
  for (var i = 0; i < document.pendingForm.elements.length; i++) {
    var e = document.pendingForm.elements[i];
    if ((e.name != 'selectall') && (e.type == 'checkbox')) {
e.checked = document.pendingForm.selectall.checked;
    }
  }
}
</SCRIPT>
					<div id='midweekTForm'>
					<b>Give pending credit</b><br/>
					Use this to give credit to brothers and pledges who have attended this midweek this week. 
					This will also open up the midweek for next week's sign up.
					<form name='pendingForm' action='midweekPendingToDB.php?midweek_name=$midweekName' method='post'>";
						for($i=0; $i<$numberPeople; $i+=1){
						echo "<label for='person$i'>$personFName[$i] $personLName[$i] </label>
							<input type='checkbox' name='person$i' value='Yes'/><br/>";
						}
						echo "<label for='selectall'>Select All</label><input type='checkbox' name='selectall' value='selected' onclick='un_check()'/><br/>
						<input type='submit' value='Give pending credit' class='loginButton'/><input type='reset' class='loginButton'/>
						</form></div>";
				} 
				if($alreadySignedUp==0){ 
				echo "<div id='midweekTForm'><form action='addToMidweek.php?midweek_name=$midweekName' method='post'>
				<b>Add yourself here</b></br>
				<label for='phoneSelf'>Phone Number (if blank, uses your stored #): </label>
				<input type='text' id='phoneSelf' name='phoneSelf'/>
				<br/>
				Can you drive?
				<input type='radio' id='drive' name='drive' value='yes'/> Yes
				<input type='radio' id='drive' name='drive' value='no' checked/> No<br/>
				<input type='submit' value='Submit' class='loginButton'/><input type='reset' class='loginButton'/>
				</form>
				</div>";
				} else {
					echo "<div id='midweekTForm'>
						You are signed up for this midweek. Click <a href='removeFromMidweek.php?midweek_name=$midweekName'>here</a> to remove yourself.</div>";
				}
				echo "<div id='midweekTForm'>
					<b> Add a brother/pledge to the midweek </b><form action='addToMidweek.php?midweek_name=$midweekName' method='post'>
					<label for='userID' id='mlAddBPLab'>Email ID of brother/pledge to be added: </label>
					<input type='text' id='userID' name='userID'/>
					<br/>
					<label for='phoneUser'>Phone Number (if blank, uses their stored #): </label>
					<input type='text' id='phoneUser' name='phoneUser'/>
					<br/>
					Can they drive?
					<input type='radio' id='drive' name='drive' value='yes'/> Yes
					<input type='radio' id='drive' name='drive' value='no' checked/> No<br/>
					<input type='submit' value='Submit' class='loginButton'/><input type='reset' class='loginButton'/>
					</form>";
			
			}else {
				if($expired==1){
					echo "<div id='midweekTForm'>Sorry, since it is within 24 hours of the start of the midweek, registration is closed. Please contact one of the midweek leaders to be added to the list.";
				} else {
					if ($closedBool==1){
						echo "<div id='midweekTForm'>Sorry, but you cannot sign up for this midweek yet. The midweek leader needs to submit the previous week's credit. Please contact one of the midweek leaders.";
				} else if($availableSlot==0){
						echo "<div id='midweekTForm'>Sorry, there are no more spots for this midweek. Please contact one of the midweek leaders to see if you can be added to the midweek.";
				} else {	
					if($alreadySignedUp==0){
					    if(strcmp($error,"1")==0){
							echo "<div id='midweekTWrong'><b>Sorry, You entered the phone number in the wrong format. Please try again.</b></div>";
						}
						echo "<div id='midweekTForm'><form action='addToMidweek.php?midweek_name=$midweekName' method='post'>
							<b>Add yourself here</b></br>
							<label for='phoneSelf'>Phone Number (if blank, uses your stored #): </label>
							<input type='text' id='phoneSelf' name='phoneSelf'/>
							<br/>
							Can you drive?
							<input type='radio' id='drive' name='drive' value='yes'/> Yes
							<input type='radio' id='drive' name='drive' value='no' checked/> No<br/>
							<input type='submit' value='Submit' class='loginButton'/><input type='reset' class='loginButton'/>
						</form>";
					} else {
						echo "<div id='midweekTForm'>
							You are signed up for this midweek. Click <a href='removeFromMidweek.php?midweek_name=$midweekName'>here</a> to remove yourself.";
					}
				}
			}
		}
			echo "</div>
			</div>
	  <b class='b4f'></b><b class='b3f'></b><b class='b2f'></b><b class='b1f'></b>
		</div>
	</center>
</font>
</body>
</head>
</html>";

	require("footer.php");
	footer();
	mysql_close($link);

?>
